import * as serviceApi from '@/services/receivable';

// 默认查询条件
const defaultCond = {
  isRecLock: -1,
  isLock: -1,
  isBillLock: 0,
  pageNumber: 1,
  pageSize: 20,

};

export default {
  namespace: 'receivable',
  state: {
    data: [],
    qc: defaultCond,
  },

  effects: {
    *get({ payload, callback }, { call, put, select }) {

      // 如果不是管理员或者操作员，则加入UserId这个查询条件，只显示当前用户的单据.
      const cUser = yield select(state => state.user.currentUser);
      const isExist = ["SysAdmin", "SysOperator"].indexOf(cUser.sys_Role.enCode);
      if (isExist < 0) {
        payload = {
          ...payload,
          userId: cUser.id
        };
      }

      const res = yield call(serviceApi.get, payload);
      if (callback) callback(res);
      yield put({
        type: 'save',
        payload: res,
      });
      yield put({
        type: 'saveQc',
        payload: payload,
      });

    },
    *reSetQC({ payload, callback }, { call, put, select }) {

      // 如果不是管理员或者操作员，则加入UserId这个查询条件，只显示当前用户的单据.
      const cUser = yield select(state => state.user.currentUser);
      const isExist = ["SysAdmin", "SysOperator"].indexOf(cUser.sys_Role.enCode);
      if (isExist < 0) {
        payload = {
          ...payload,
          userId: cUser.id
        };
      }

      const res = yield call(serviceApi.get, {
        ...defaultCond,
        ...payload
      });

      yield put({
        type: 'save',
        payload: res,
      });
      yield put({
        type: 'saveQc',
        payload: defaultCond,
      });
      if (callback) callback(res);
    },
    *receipt({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.receipt, payload);
      if (callback) callback(res);
    },
    *getReceiptByBillId({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.getReceiptByBillId, payload);
      if (callback) callback(res);
    },
    *changeTiDanState({ payload, callback }, { call, put }) {
      const res = yield call(serviceApi.changeTiDanState, payload)
      if (callback)
        callback(res);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveQc(state, action) {
      return {
        ...state,
        qc: action.payload,
      };
    },
  },
};
